package uestc.lj.gulishop.order.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import uestc.lj.common.exception.NoStockException;
import uestc.lj.gulishop.order.entity.OrderEntity;
import uestc.lj.gulishop.order.service.OrderService;
import uestc.lj.gulishop.order.vo.OrderConfirmVO;
import uestc.lj.gulishop.order.vo.OrderSubmitVO;
import uestc.lj.gulishop.order.vo.submitOrderResponseVO;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

/**
 * @Author:Crazlee
 * @Date:2021/6/15
 */
@Controller
@Slf4j
public class OrderFontController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 购物车中去结算功能
     *
     * @param model
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVO orderConfirmVO = orderService.confirmOrder();
        model.addAttribute("orderConfirmData", orderConfirmVO);
        return "confirm";
    }

    /**
     * 提交订单即下单的功能
     *
     * @param orderSubmitVO
     * @return
     */
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVO orderSubmitVO, Model model, RedirectAttributes redirectAttributes) {
        try {
            submitOrderResponseVO submitOrderResponseVO = orderService.submitOrder(orderSubmitVO);
            log.info("订单提交的数据：" + orderSubmitVO);
            if (submitOrderResponseVO.getCode() == 200) {
                // 订单提交成功,转到支付选择页
                model.addAttribute("submitOrderResponse", submitOrderResponseVO);
                return "pay";
            } else {
                // 订单提交失败，跳回支付页
                String message = "下单失败：";
                switch (submitOrderResponseVO.getCode()) {
                    case 1:
                        message += "订单信息已过期，请刷新页面重新购买";
                        break;
                    case 2:
                        message += "购物项中有些商品价格发生变化，请返回购物车查看";
                        break;
                    default:
                        message += "您购买的商品库存暂时不够，请调整您要购买的数量";
                        break;
                }
                redirectAttributes.addFlashAttribute("message", message);
                return "redirect:http://order.gulishop.com/toTrade";
            }
        } catch (Exception e) {
            if (e instanceof NoStockException) {
                String message = e.getMessage();
                redirectAttributes.addFlashAttribute("message", message);
            }
            return "redirect:http://order.gulishop.com/toTrade";
        }
    }


    /**
     * 测试延迟消息队列
     * @return
     */
    @GetMapping("/test/createOrder")
    @ResponseBody
    public String createOrderTest() {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(UUID.randomUUID().toString());
        orderEntity.setModifyTime(new Date());

        rabbitTemplate.convertAndSend("order-event-exchange", "order.create.order", orderEntity);
        return "ok";
    }
}
